package com.estar.integaration.Controller.paymentPlanController;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.estar.integaration.Service.paymentPlanService.ISupplierAccountMultipleSituationService;
import com.estar.integaration.Service.paymentPlanService.ISupplierAccountPeriodService;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.NoSQL.Res;
import com.estar.integaration.entity.paymentPlanEntity.SupplierAccountMultipleSituation;
import com.alibaba.fastjson.JSONObject;
import com.estar.integaration.entity.paymentPlanEntity.SupplierAccountPeriod;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YJL
 * @since 2022-03-08
 */
@RestController
@RequestMapping("/supplier-account-multiple-situation")
public class SupplierAccountMultipleSituationController {


  @Autowired
  private ISupplierAccountMultipleSituationService supplierAccountMultipleSituationService;

  @Autowired
  private ISupplierAccountPeriodService supplierAccountPeriodService;

  @PostMapping("/update_situation")
  public Res updateDay(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("sName");
    JSONArray jsonArray = jsonObject.getJSONArray("percentage_day_content");
    String type = jsonObject.getString("haveIn");

    List<SupplierAccountMultipleSituation> supplierAccountMultipleSituationList = new ArrayList<>();
    for(int i =0; i < jsonArray.size() ; i++){
      JSONObject jO = jsonArray.getJSONObject(i);
      Double percentage = jO.getDouble("percentage");
      String needAddDay = jO.getString("need_add_day");
      SupplierAccountMultipleSituation supplierAccountMultipleSituation = new SupplierAccountMultipleSituation(Util.getUUID(),supplierName,percentage,needAddDay,type);
      supplierAccountMultipleSituationList.add(supplierAccountMultipleSituation);
    }
    supplierAccountMultipleSituationService.removeListBySupplierNameandType(supplierName,type);//删除此供应商的关于有入库或无入库的所有的多情况
    //全部删除后再次保存
    supplierAccountMultipleSituationService.saveBatch(supplierAccountMultipleSituationList);
    return new Res("20000","修改成功");
  }

  /**
   *
   * @param jsonObject
   * @return
   */
  @PostMapping("/set_only_percentage")
  public Res savePercentageSituation(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("supplierName");
    String percentage = jsonObject.getString("percentage");
    SupplierAccountMultipleSituation supplierAccountMultipleSituation = new SupplierAccountMultipleSituation(Util.getUUID(),supplierName,Double.parseDouble(percentage),"","percentage");
    supplierAccountMultipleSituationService.save(supplierAccountMultipleSituation);
    return new Res("20000","提交成功");
  }

  /**
   * 获取此物料的纯百分比
   * @return
   */
  @PostMapping("/get_only_percentage")
  public Res setOnlyPercentageSituation(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("supName");
    SupplierAccountMultipleSituation supplierAccountMultipleSituation = supplierAccountMultipleSituationService.getSPAMSwithPercentageTypebySuppName(supplierName,"percentage");
    String percentage = null;
    if(supplierAccountMultipleSituation == null){
      percentage = "";
    }else {
      percentage = String.valueOf(supplierAccountMultipleSituation.getPercentage());
    }
    return new Res("20000",percentage);
  }


  /**
   * 获得某个供应商下的所有日期
   * @param jsonObject
   * @return
   */
  @PostMapping("/get_situation")
  public Res getSituation(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("supplierName");
    String type = jsonObject.getString("haveIn");
    List<SupplierAccountMultipleSituation> supplierAccountMultipleSituationList = new ArrayList<>();
    supplierAccountMultipleSituationList = supplierAccountMultipleSituationService.getSupplierAccountMultipleSituationBySupplierName(supplierName);
    if(type.equals("have")){
      supplierAccountMultipleSituationList = supplierAccountMultipleSituationList.stream().filter(s -> s.getIfHaveInventoryNumber().equals("have")).collect(Collectors.toList());
    }else if(type.equals("no")){
      supplierAccountMultipleSituationList = supplierAccountMultipleSituationList.stream().filter(s -> s.getIfHaveInventoryNumber().equals("no")).collect(Collectors.toList());
    }
    return new Res("20000",new Gson().toJson(supplierAccountMultipleSituationList));
  }


  /**
   * 删除某一个供应商账期
   */
  @PostMapping("/delete_situation")
  public Res deleteSituation(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("supName");
    supplierAccountMultipleSituationService.deleteBySupplierName(supplierName);
    supplierAccountPeriodService.deleteBySupplierName(supplierName);
    List<SupplierAccountPeriod> supplierAccountMultipleSituationList = supplierAccountPeriodService.getAllSupplierAccountPeriod();
    return new Res("20000",new Gson().toJson(supplierAccountMultipleSituationList));
  }

}
